package com.china.milkywayeye.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import com.china.milkywayeye.service.AliyunService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.*;

/**
 * @ClassName AliyunServiceImpl
 * @Description TODO
 * @Author milkywayeye
 * @Date 2023/12/28 17:35
 * @Version V1.0
 **/
@Service
public class AliyunServiceImpl implements AliyunService {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    private String line;
    private BufferedReader reader;

    public void ootd() {
        System.out.println("我是阿里云服务集群");
    }

    @Override
    public String toString() {
        return "AliyunServiceImpl{" +
                "endpoint='" + endpoint + '\'' +
                ", accessKeyId='" + accessKeyId + '\'' +
                ", accessKeySecret='" + accessKeySecret + '\'' +
                ", bucketName='" + bucketName + '\'' +
                '}';
    }

    public void download() {
        OSS ossClient = null;
        try {
            String FilePath = "./test/";
            File file = new File(FilePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint
                    , accessKeyId
                    , accessKeySecret);
            DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, "panoramicImages/GuangMingPanoramicImage60/D13100/pano.xml");
            downloadFileRequest.setDownloadFile(FilePath + "pano.xml");
            // 下载文件。
            DownloadFileResult downloadRes = ossClient.downloadFile(downloadFileRequest);
            // 下载成功时，会返回文件元信息。
            ObjectMetadata objectMetadata = downloadRes.getObjectMetadata();

            change3();

            UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, "panoramicImages/GuangMingPanoramicImage60/D13100/pano.xml");
            uploadFileRequest.setUploadFile("test/pano1.xml");
            ossClient.uploadFile(uploadFileRequest);
        } catch (Exception e) {
            System.out.println("下载有问题");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            //资源关闭
            assert ossClient != null;
            ossClient.shutdown();
        }
    }

    //在指定字符串之前插入内容
    public void change() throws IOException {
        File file = new File("./test/pano.xml");
        File tempFile = new File("./test/pano1.xml");
        reader = new BufferedReader(new FileReader(file));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

        String searchStr = "</krpano>";
        String insertContent = "蛙哈哈哈哈";
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.contains(searchStr)) {
                // 找到要插入内容的位置，可以在此处插入内容
                writer.write(insertContent);
                writer.newLine();
            }
            writer.write(line);
            writer.newLine();
        }

//        //重命名
//        tempFile.renameTo(file);

        reader.close();
        writer.close();
    }

    //在指定字符串之后插入内容
    public void change1() throws IOException {
        File file = new File("./test/pano.xml");
        File tempFile = new File("./test/pano1.xml");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

        String searchStr = "</krpano>";
        String insertContent = "要插入的内容";
        String line;
        boolean found = false;
        while ((line = reader.readLine()) != null) {
            writer.write(line);
            writer.newLine();
            if (line.contains(searchStr)) {
                // 找到</krpano>之后的位置，可以在此处插入内容
                writer.write(insertContent);
                writer.newLine();
                found = true;
            }
        }

        if (!found) {
            // 如果</krpano>未找到，则在文件末尾插入内容
            writer.write(insertContent);
            writer.newLine();
        }

        reader.close();
        writer.close();
    }

    //在指定字符串上一行插入内容
    public void change2() throws IOException {
        File file = new File("./test/pano.xml");
        File tempFile = new File("./test/pano1.xml");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

        String searchStr = "</krpano>";
        String insertContent = "要插入的内容";
        String line;
        String prevLine = null;
        while ((line = reader.readLine()) != null) {
            if (prevLine != null) {
                writer.write(prevLine);
                writer.newLine();
            }
            prevLine = line;
            if (line.contains(searchStr)) {
                // 找到</krpano>所在行的上一行位置，可以在此处插入内容
                writer.write(insertContent);
                writer.newLine();
            }
        }

        writer.write(prevLine); // 写入文件的最后一行

        reader.close();
        writer.close();
    }

    //在指定字符串下一行插入内容
    public void change3() throws IOException {
        File file = new File("./test/pano.xml");
        File tempFile = new File("./test/pano1.xml");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

        String searchStr = "<include url=\"skin/defaultskin.xml\" />";
        String insertContent = "<action name=\"mytest\"> screentosphere(mouse.x, mouse.y, toh, tov); jscall(calc('console.log(\"'+tov+'\")')); jscall(calc('console.log(\"'+toh+'\")')); </action>\n" +
                "<events onclick=\"mytest();\"/>";
        String line;
        while ((line = reader.readLine()) != null) {
            writer.write(line);
            writer.newLine();
            if (line.contains(searchStr)) {
                // 找到</krpano>所在行的下一行位置，可以在此处插入内容
                writer.write(insertContent);
                writer.newLine();
            }
        }

        reader.close();
        writer.close();
    }
}
